package mylittleinvestigations.GameUi;

import mylittleinvestigations.MouseHelper;
import mylittleinvestigations.PersistentThreads.RenderThread;
import mylittleinvestigations.Sprite;
import mylittleinvestigations.Utils;
import mylittleinvestigations.Vector2;
import org.dawnmew.jpurewave.SoundSystem;
import org.newdawn.slick.*;
import org.newdawn.slick.geom.Rectangle;

public class Tab
{
    public static final int Width = 183; // px
    public static final int Height = 38; // px
    
    private static final Color NormalTextColor = Color.white;
    private static final Color MouseOverTextColor = Color.yellow;
    private static final Color MouseDownTextColor = Color.red;
    private static final Color DisabledTextColor = Color.gray;

    private static int dialogRowYPosition;
    private static int bottomRowYPosition;
    private static Image downTexture;
    private static Image upTexture;
    private static int width;
    private static int height;
    private static int textAreaWidth;
    private static int textAreaHeight;
    private static UnicodeFont font;

    private int xPosition;
    private TabRow row;
    private Rectangle areaRect;

    private boolean isClickable;
    private boolean isMouseOver;
    private boolean isMouseDown;

    private String clickSoundEffect = "";

    private String text;
    
    public String getText() { return this.text; }
    public void setText(String text) { this.text = text; }

    public boolean isEnabled;
    
    public boolean getIsEnabled() { return this.isEnabled; }
    public void setIsEnabled(boolean isEnabled) { this.isEnabled = isEnabled; }

    public boolean isClicked;
    
    public boolean getIsClicked() { return this.isClicked; }
    public void setIsClicked(boolean isClicked) { this.isClicked = isClicked; }

    public boolean useCancelClickSoundEffect;
    
    public boolean getUseCancelClickSoundEffect() { return this.useCancelClickSoundEffect; }
    public void setUseCancelClickSoundEffect(boolean useCancelClickSoundEffect) { this.useCancelClickSoundEffect = useCancelClickSoundEffect; }

    public String getClickSoundEffect()
    {
        if (this.clickSoundEffect == null || this.clickSoundEffect.equals(""))
        {
            this.clickSoundEffect = (this.getUseCancelClickSoundEffect() ? "ButtonClick4" : "ButtonClick1");
        }

        return this.clickSoundEffect;
    }
    
    private Sprite tabImage;
    
    public Sprite getTabImage() { return this.tabImage; }
    public void setTabImage(Sprite tabImage) { this.tabImage = tabImage; }

    public static void Initialize(int dialogRowYPosition, int bottomRowYPosition, Image downTexture, Image upTexture, UnicodeFont font)
    {
        Tab.dialogRowYPosition = dialogRowYPosition;
        Tab.bottomRowYPosition = bottomRowYPosition;
        Tab.downTexture = downTexture;
        Tab.upTexture = upTexture;
        Tab.width = downTexture.getWidth();
        Tab.height = downTexture.getHeight();
        Tab.textAreaWidth = downTexture.getWidth() - 6;
        Tab.textAreaHeight = downTexture.getHeight() - 6;
        Tab.font = font;
    }

    public Tab(int xPosition, boolean isClickable)
    {
        this(xPosition, isClickable, "", false);
    }

    public Tab(int xPosition, boolean isClickable, String text)
    {
        this(xPosition, isClickable, text, false);
    }

    public Tab(int xPosition, boolean isClickable, String text, boolean useCancelClickSoundEffect)
    {
        this(xPosition, isClickable, text, useCancelClickSoundEffect, TabRow.Dialog);
    }

    public Tab(int xPosition, boolean isClickable, String text, boolean useCancelClickSoundEffect, TabRow row)
    {
        this.xPosition = xPosition;
        this.row = row;
        this.areaRect = new Rectangle(this.xPosition, this.GetYPositionFromRow(), width, height);
        this.isClickable = isClickable;

        this.text = text;
        this.isEnabled = true;
        this.useCancelClickSoundEffect = useCancelClickSoundEffect;
    }

    public void Update()
    {
        if (this.isClickable && this.getIsEnabled())
        {
            this.isMouseOver = MouseHelper.MouseOverRect(areaRect);
            this.isMouseDown = MouseHelper.MouseDownOnRect(areaRect);
            this.setIsClicked(MouseHelper.ClickedOnRect(areaRect));

            if (this.isMouseOver || this.isMouseDown)
            {
                MouseHelper.SetToHandCursor();
            }

            if (this.getIsClicked())
            {
                SoundSystem.getInstance().playSound(getClickSoundEffect());
            }
        }
    }

    public void Draw(GameContainer gc, Graphics g)
    {
        this.Draw(gc, g, 0);
    }

    public void Draw(GameContainer gc, Graphics g, float yOffset)
    {
        if (this.getText() != null & !Utils.equals(this.getText(), ""))
        {
            Color textColor;

            if (!this.getIsEnabled() && this.isClickable)
            {
                textColor = DisabledTextColor;
            }
            else if (this.isMouseDown)
            {
                textColor = MouseDownTextColor;
            }
            else if (this.isMouseOver)
            {
                textColor = MouseOverTextColor;
            }
            else
            {
                textColor = NormalTextColor;
            }

            int yPosition = this.GetYPositionFromRow() + (int)yOffset;
            Vector2 centerPoint = new Vector2(this.xPosition + 3 + textAreaWidth / 2, yPosition + textAreaHeight / 2);
            Vector2 textPosition = new Vector2(centerPoint.getX() - font.getWidth(this.getText()) / 2, yPosition + 1);

            if (this.getTabImage() != null)
            {
                textPosition.setX(textPosition.getX() + this.getTabImage().getWidth() / 2);
            }
            
            RenderThread.getInstance().AddImageToRenderQueue(this.row == TabRow.Top ? upTexture : downTexture, xPosition, yPosition);
            RenderThread.getInstance().AddStringToRenderQueue(font, (float)textPosition.getX(), (float)textPosition.getY(), this.getText(), textColor);

            if (this.getTabImage() != null)
            {
                this.getTabImage().Draw(gc, g, new Vector2(textPosition.getX() - this.getTabImage().getWidth() - 4, downTexture.getHeight() - 3 - this.getTabImage().getHeight()));
            }
        }
    }

    public void Reset()
    {
        this.isMouseOver = false;
        this.isMouseDown = false;
        this.isClicked = false;
    }

    private int GetYPositionFromRow()
    {
        switch (this.row)
        {
            case Top:
                return 0;

            case Dialog:
                return dialogRowYPosition;

            case Bottom:
                return bottomRowYPosition;

            default:
                return 0;
        }
    }
}